package com.mdl.springmvc.ioStream.io;

import java.io.*;

/**
 * @Desc
 * @Author madengling
 * @Time 2020/12/29 16:41
 */
public class OutputStreamWriterTest08 {
    private static final String PATH = "G:\\dhcc\\idea\\MavenProject\\src\\main\\webapp\\files\\d\\";
    public static void main(String[] args) {
        // 定义文件路径
        String FileName = PATH+"s.txt";
        BufferedWriter bw = null;
        try {
            // 创建流对象,默认UTF8编码
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(FileName)));
            // 写出数据
            bw.write("中文"); // 保存为6个字节
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // 定义文件路径
        String FileName2 = PATH+"t.txt";
        try {
            // 创建流对象,指定GBK编码
            // 为了达到最高效率，可以在BufferWriter中包装OutputStreamWriter
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(FileName2),"GBK"));
            // 写出数据
            bw.write("测试");// 保存为4个字节
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("写入成功");
    }
}
